package com.allen.library.utils;

import android.util.Log;


import com.alibaba.fastjson.JSONObject;
import com.allen.library.base.BaseResponse;

import com.allen.library.gson.GsonUtil;




import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

public class upload {
    public DataCallBack mDataCallBack;





    //数据回调接口
    public interface DataCallBack<T> {
        void onDataSuccessCallBack( T t);
        void onDataFailCallBack(T t);
    }

    public static String uploadVoice(String uploadUrl, String FileName,String Json,byte[] lens,DataCallBack<BaseResponse>callBack) {
        try {

            URL url = new URL(uploadUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setConnectTimeout(10000);
            conn.setReadTimeout(10000);
            // 设置请求头参数
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("Charsert", "UTF-8");
            conn.setRequestProperty("Token", "352625064382573");
            conn.setRequestProperty("Content-Type", "multipart/form-data");
            OutputStream out = new DataOutputStream(conn.getOutputStream());
            Log.d("uploadVoice",Json);
            out.write(lens, 0, lens.length);
            out.write(Json.getBytes("UTF-8"), 0, Json.getBytes("UTF-8").length);
            File file =new File(FileName);
            if (file.exists()){
                InputStream inputStream = new FileInputStream(new File(FileName));
                // 数据输入流,用于读取文件数据
                DataInputStream in = new DataInputStream(inputStream);
                byte[] bufferOut = new byte[4096];
                int bytes = 0;
                // 每次读1KB数据,并且将文件数据写入到输出流中
                while ((bytes = in.read(bufferOut)) != -1) {
                    out.write(bufferOut, 0, bytes);
                }
                out.flush();
                out.close();
                //获取返回码，根据返回码做相应处理
                int res = conn.getResponseCode();
                Log.d("response code", "response code:" + res);

                BufferedReader input = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                StringBuilder result = new StringBuilder();
                String line;
                while ((line = input.readLine()) != null) {
                    result.append(line);
                }

                String parse = (String) JSONObject.parse(result.toString());

                BaseResponse baseResponse = GsonUtil.getGson().fromJson(parse, BaseResponse.class);



                if (baseResponse.getResultCode()==1){
                    //上传成功
                    callBack.onDataSuccessCallBack(baseResponse);
                }else{
                    callBack.onDataFailCallBack(baseResponse);

                }

                return result.toString();


            }


//            // 最后添加换行
//            out.write(newLine.getBytes());
////            in.close();
//            // 定义最后数据分隔线，即--加上boundary再加上--。
//            byte[] end_data = (newLine + boundaryPrefix + boundary + boundaryPrefix + newLine).getBytes();
//            // 写上结尾标识
//            out.write(end_data);

//            // 定义BufferedReader输入流来读取URL的响应
//            StringBuffer sbOutPut=new StringBuffer();
//            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
//            String line = null;
//            while ((line = reader.readLine()) != null) {
//                sbOutPut.append(line);
//            }
//
//            Log.d("kkkkkkk",sbOutPut.toString());
//            return sbOutPut.toString();

        } catch (Exception e) {
            BaseResponse baseResponse =new BaseResponse();
            baseResponse.setError("發送Post請求出現異常！" + e);
            callBack.onDataFailCallBack(baseResponse);

            e.printStackTrace();
            return null;
        }
        return null;
    }
    public static String uploadPhoto(String uploadUrl, String FileName,String Json,byte[] lens,DataCallBack<BaseResponse>callBack) {
        try {
            URL url = new URL(uploadUrl);
            Log.d("uploadPhoto",Json);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setConnectTimeout(10000);
            conn.setReadTimeout(10000);
            // 设置请求头参数
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("Charsert", "UTF-8");
            conn.setRequestProperty("Token", "352625064382573");
            conn.setRequestProperty("Content-Type", "multipart/form-data");
            OutputStream out = new DataOutputStream(conn.getOutputStream());

            out.write(lens, 0, lens.length);
            out.write(Json.getBytes("UTF-8"), 0, Json.getBytes("UTF-8").length);
            Log.d("uploadPhoto",Json);
            File file =new File(FileName);
            if (file.exists()){
                InputStream inputStream = new FileInputStream(new File(FileName));
                // 数据输入流,用于读取文件数据
                DataInputStream in = new DataInputStream(inputStream);
                byte[] bufferOut = new byte[4096];
                int bytes = 0;
                // 每次读1KB数据,并且将文件数据写入到输出流中
                while ((bytes = in.read(bufferOut)) != -1) {
                    out.write(bufferOut, 0, bytes);
                }
                out.flush();
                out.close();
                //获取返回码，根据返回码做相应处理
                int res = conn.getResponseCode();
                Log.d("response code", "response code:" + res);

                BufferedReader input = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                StringBuilder result = new StringBuilder();
                String line;
                while ((line = input.readLine()) != null) {
                    result.append(line);
                }



                String parse = (String) JSONObject.parse(result.toString());

                BaseResponse baseResponse = GsonUtil.getGson().fromJson(parse, BaseResponse.class);



                if (baseResponse.getResultCode()==1){
                    //上传成功
                    callBack.onDataSuccessCallBack(baseResponse);
                }else{
                    callBack.onDataFailCallBack(baseResponse);

                }

                return result.toString();


            }


//            // 最后添加换行
//            out.write(newLine.getBytes());
////            in.close();
//            // 定义最后数据分隔线，即--加上boundary再加上--。
//            byte[] end_data = (newLine + boundaryPrefix + boundary + boundaryPrefix + newLine).getBytes();
//            // 写上结尾标识
//            out.write(end_data);

//            // 定义BufferedReader输入流来读取URL的响应
//            StringBuffer sbOutPut=new StringBuffer();
//            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
//            String line = null;
//            while ((line = reader.readLine()) != null) {
//                sbOutPut.append(line);
//            }
//
//            Log.d("kkkkkkk",sbOutPut.toString());
//            return sbOutPut.toString();

        } catch (Exception e) {
            BaseResponse baseResponse =new BaseResponse();
            baseResponse.setError("發送Post請求出現異常！" + e);
            callBack.onDataFailCallBack(baseResponse);
            System.out.println("發送Post請求出現異常！" + e);
            e.printStackTrace();
            return null;
        }
        return null;
    }

}
